// created on 2/1/2006 at 7:57 AM
using System;
using System.Collections;

namespace Whitemice.MOGI
{
	/// \brief This is a container class for IAppointment objects
	public class AppointmentList : ArrayList
	{
		protected IServer	server;
		/// \brief Constructor
		public AppointmentList() : base()
		{
		}
		
		public AppointmentList(ICollection collection, IServer server) : base(collection)
		{
			this.server = server;
		}
		
		/// \brief Returns list of appointments on the given day
		/// \param date
		public AppointmentList Appointments(DateTime date)
		{
			DateTime start;
			DateTime end;
		
			start = date.Date;
			end = new DateTime(date.Year, date.Month, date.Day, 23, 59, 59);
			return Appointments(start, end);
		}
	
		/// \brief Returns list of appointments between two times
		/// \param start
		/// \param end
		public AppointmentList Appointments(DateTime start, DateTime end)
		{
			AppointmentList appointmentList = new AppointmentList();
			foreach(IAppointment appointment in this)
				if ((appointment.Start.ToLocalTime() < end) && (appointment.End.ToLocalTime() > start))
					appointmentList.Add(appointment);
			return appointmentList;
		}

		/// \brief Returns true if actor has appointments on given day
		/// \param date
		public bool HasAppointments(DateTime date)
		{
			if(Appointments(date).Count > 0) return true;
			Console.WriteLine("No appointments found on " + date.ToString());
			return false;
		}
	
		/// \brief Returns true if the actor has appointments between two times
		/// \param start
		/// \param end
		public bool HasAppointments(DateTime start, DateTime end)
		{
			if(Appointments(start, end).Count > 0) return true;
			return false;
		}
		
		public IAppointment Get(string id)
		{
			Console.WriteLine("AppointmentList:Get(" + id + ")");
			if (MOGI.Id(id) == "0")
			{
				Console.WriteLine("Returning Empty Appointment");
				return (IAppointment)server.Get("Document/Appointment/0");
			}
			foreach(IAppointment appointment in this)
				if (appointment.Name == id)
				{
					return appointment;
				}
			return null;
		}
	}
}
